﻿using Microsoft.Office.Core;
using System;
using System.Runtime.InteropServices;

namespace InfoPathHelpers.ToolBar
{
    /// <summary>
    /// Provides a number of methods to assist with common operations in InfoPath.
    /// </summary>
    public static class ToolBarHelpers
    {
        /// <summary>
        /// Toggles all command bars in the collection.
        /// </summary>
        /// <param name="commandBarsCollection">The command bar collection to toggle.</param>
        /// <param name="areEnabled">A boolean value indicating whether or not the command bars should be enabled.</param>
        public static void ToggleAllToolbars(object commandBarsCollection, bool areEnabled)
        {
            foreach (CommandBar CommandBar in (CommandBars)commandBarsCollection)
            {
                CommandBar.Enabled = areEnabled;
            }
        }

        /// <summary>
        /// Toggles all standard InfoPath command bars.
        /// </summary>
        /// <param name="commandBarsCollection">The command bar collection to toggle.</param>
        /// <param name="areEnabled">A boolean value indicating whether or not the command bars should be enabled.</param>
        public static void ToggleStandardToolbarsEnabled(object commandBarsCollection, bool areEnabled)
        {
            ToggleToolbarEnabled(commandBarsCollection, CommandBarNames.Ink, areEnabled);
            ToggleToolbarEnabled(commandBarsCollection, CommandBarNames.Standard, areEnabled);
            ToggleToolbarEnabled(commandBarsCollection, CommandBarNames.Tables, areEnabled);
            ToggleToolbarEnabled(commandBarsCollection, CommandBarNames.Formatting, areEnabled);
        }

        /// <summary>
        /// Toggles all standard InfoPath command bars.
        /// </summary>
        /// <param name="commandBarsCollection">The command bar collection to toggle.</param>
        /// <param name="areVisible">A boolean value indicating whether or not the command bars should be visible.</param>
        public static void ToggleStandardToolbarsVisibility(object commandBarsCollection, bool areVisible)
        {
            ToggleToolbarVisibility(commandBarsCollection, CommandBarNames.Ink, areVisible);
            ToggleToolbarVisibility(commandBarsCollection, CommandBarNames.Standard, areVisible);
            ToggleToolbarVisibility(commandBarsCollection, CommandBarNames.Tables, areVisible);
            ToggleToolbarVisibility(commandBarsCollection, CommandBarNames.Formatting, areVisible);
        }

        /// <summary>
        /// Toggles the specified command bar if it exists.
        /// </summary>
        /// <param name="commandBarsCollection">The command bar collection in which to look for the command bar.
        /// (Application.ActiveWindow.CommandBars)</param>
        /// <param name="commandBarName">The name of the command bar (use CommandBarNames class).</param>
        /// <param name="isEnabled">A boolean value indicating whether or not the command bar should be enabled.</param>
        public static void ToggleToolbarEnabled(object commandBarsCollection, string commandBarName , bool isEnabled)
        {
            var CommandBars = (CommandBars)commandBarsCollection;
            try
            {
                var CommandBar = CommandBars[commandBarName];
                CommandBar.Enabled = isEnabled;
            }
            catch (ArgumentException) { }
        }

        /// <summary>
        /// Toggles the specified command bar if it exists.
        /// </summary>
        /// <param name="commandBarsCollection">The command bar collection in which to look for the command bar.
        /// (Application.ActiveWindow.CommandBars)</param>
        /// <param name="commandBarName">The name of the command bar (use CommandBarNames class).</param>
        /// <param name="isVisible">A boolean value indicating whether or not the command bar should be enabled.</param>
        public static void ToggleToolbarVisibility(object commandBarsCollection, string commandBarName, bool isVisible)
        {
            var CommandBars = (CommandBars)commandBarsCollection;
            try
            {
                var CommandBar = CommandBars[commandBarName];
                CommandBar.Visible = isVisible;
            }
            catch (ArgumentException) { }
            catch (COMException) { }
        }
    } 
}
